package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Category;
import com.itheima.domain.CategoryExample;
import com.itheima.domain.Movies;
import com.itheima.domain.MoviesExample;
import com.itheima.mapper.CategoryMapper;
import com.itheima.mapper.MoviesMapper;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Override
    public PageInfo<Category> findByPage(Integer pageNum, Integer pageSize, String keyword) {
        //开启分页
        PageHelper.startPage(pageNum,pageSize);
        //判断是否有关键字搜索
        if (keyword!=null){
            CategoryExample categoryExample = new CategoryExample();
            CategoryExample.Criteria criteria = categoryExample.createCriteria();
            criteria.andCategoryNameLike("%"+keyword+"%");
            List<Category> list = categoryMapper.selectByExample(categoryExample);
            return new PageInfo<Category>(list);
        }
        CategoryExample categoryExample = new CategoryExample();
        List<Category> list = categoryMapper.selectByExample(categoryExample);
        return new PageInfo<Category>(list);
    }

    @Override
    public void save(Category category) {
        //调用mapper
        categoryMapper.insert(category);
    }

    @Override
    public Category findById(Integer id) {
        return categoryMapper.findById(id);
    }

    @Override
    public void deleteById(Integer id) {
        categoryMapper.deleteById(id);
    }

    @Override
    public void deleteIds(Integer[] ids) {
        categoryMapper.deleteIds(ids);
    }
    @Autowired
    private MoviesMapper moviesMapper;
    @Override
    public Map<String, List<Movies>> findAllCategoryAndMovies() {
        //新建一个Map
        HashMap<String, List<Movies>> cateAndMovieMap = new HashMap<>();
        //查询所有分类
        CategoryExample categoryExample = new CategoryExample();
        categoryExample.setOrderByClause("order_no asc");
        List<Category> categoryList = categoryMapper.selectByExample(categoryExample);
        //遍历所得的分类集合，从每一个分类中查询该分类下所有的电影，并存入List
        for (Category category : categoryList) {
            PageHelper.startPage(1,6);
            MoviesExample moviesExample = new MoviesExample();
            MoviesExample.Criteria criteria = moviesExample.createCriteria();
            moviesExample.setOrderByClause("show_time desc");
            criteria.andCidEqualTo(category.getId());
            List<Movies> moviesList = moviesMapper.selectByExample(moviesExample);
            cateAndMovieMap.put(category.getCategoryName(),moviesList);
        }
        //返回Map
        return cateAndMovieMap;
    }

    @Override
    public List<Category> findAll() {
        CategoryExample categoryExample = new CategoryExample();
        return categoryMapper.selectByExample(categoryExample);
    }


}
